package com.ninong.ker.admin.service.impl;

import java.util.List;

import com.ninong.ker.admin.domain.*;
import com.ninong.ker.admin.mapper.*;
import com.ninong.ker.admin.service.FwTenantService;
import com.ninong.ker.admin.service.dto.FwTenantDTO;
import com.ninong.ker.common.auth.SecurityAdmin;
import com.ninong.ker.common.user.OnlineUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * @author jmz
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FwTenantServiceImpl extends ServiceImpl<FwTenantMapper, FwTenant> implements FwTenantService {

	private static final String MENU = "menu";
	@Autowired
	private MenuMapper menuMapper;
	@Autowired
	private DicMapper dicMapper;
	@Autowired
	private DicDetailMapper dicDetailMapper;
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private FwTenantUserMapper tenantUserMapper;
	@Autowired
	private SecurityAdmin securityAdmin;

    @Override
	public PageInfo<FwTenant> listPage(FwTenantDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		QueryWrapper<FwTenant> query = new QueryWrapper<>();
		List<FwTenant> fwTenants = baseMapper.selectList(query);
		return new PageInfo<FwTenant>(fwTenants);
	}

	@Override
	public void create(FwTenant entity) {
		OnlineUser onlineUser = securityAdmin.currentUser();
		entity.setCreator(onlineUser.getId());
		saveOrUpdate(entity);
		FwTenantUser tenantUser = new FwTenantUser(entity.getId(),onlineUser.getId(),true);
		// 默认创建团队的人就是管理员
		tenantUserMapper.insert(tenantUser);
		// 复制字典
		Dic dic = new Dic("type","菜单类型",entity.getId());
		dicMapper.insert(dic);
		// 复制字典明细
		DicDetail detail = new DicDetail("menu","菜单",dic.getId(),entity.getId());
		dicDetailMapper.insert(detail);
		detail = new DicDetail("button","按钮",dic.getId(),entity.getId());
		dicDetailMapper.insert(detail);
		dic = new Dic("logType","日志类型",entity.getId());
		dicMapper.insert(dic);
		detail = new DicDetail("INFO","INFO",dic.getId(),entity.getId());
		dicDetailMapper.insert(detail);
		detail = new DicDetail("ERROR","ERROR",dic.getId(),entity.getId());
		dicDetailMapper.insert(detail);
		// 创建一个默认不参与业务的角色
		Role role = new Role("平台管理员",entity.getId());
		roleMapper.insert(role);
		// 把用户中心的角色管理赋给  其他让平台管理员自己分配
		RoleMenu roleMenu = new RoleMenu("2",role.getId(),entity.getId());
		roleMenuMapper.insert(roleMenu);
		roleMenu = new RoleMenu("52fcdeed7b82cd6126c5cc4c71284556",role.getId(),entity.getId());
		roleMenuMapper.insert(roleMenu);
		roleMenu = new RoleMenu("452b15dd4df79d578e0b7d2cb9136a14",role.getId(),entity.getId());
		roleMenuMapper.insert(roleMenu);
		roleMenu = new RoleMenu("6ea3a17c9cebadb222fc05ada1ab6d5f",role.getId(),entity.getId());
		roleMenuMapper.insert(roleMenu);
		roleMenu = new RoleMenu("95f261d5a57cfe2e5839a9ae61fa3926",role.getId(),entity.getId());
		roleMenuMapper.insert(roleMenu);
		roleMenu = new RoleMenu("a23a2b4fc2a545a5df2915637ca4aa2e",role.getId(),entity.getId());
		roleMenuMapper.insert(roleMenu);
		// 默认角色绑定当前人
		UserRole userRole = new UserRole(onlineUser.getId(),role.getId(),entity.getId());
		userRoleMapper.insert(userRole);
	}
}